/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.spring.tracing;

import com.jakewharton.nopen.annotation.Open;

import io.sentry.Breadcrumb;
import io.sentry.IHub;
import io.sentry.ISpan;
import io.sentry.SentryTraceHeader;
import io.sentry.SpanStatus;
import io.sentry.util.Objects;

import java.io.IOException;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

/**
 * SentrySpanClientHttpRequestInterceptor
 *
 * @since 2021-07-29
 */
@Open
public class SentrySpanClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
    private final @NotNull IHub hub;

    /**
     * constructor
     *
     * @param hub IHub for interceptor
     */
    public SentrySpanClientHttpRequestInterceptor(final @NotNull IHub hub) {
        this.hub = Objects.requireNonNull(hub, "hub is required");
    }

    @Override
    public @NotNull ClientHttpResponse intercept(
        @NotNull HttpRequest request,
        @NotNull byte[] body,
        @NotNull ClientHttpRequestExecution execution)
        throws IOException {
        Integer responseStatusCode = null;
        try {
            final ISpan activeSpan = hub.getSpan();
            if (activeSpan == null) {
                return execution.execute(request, body);
            }

            final ISpan span = activeSpan.startChild("http.client");
            span.setDescription(request.getMethodValue() + " " + request.getURI());

            final SentryTraceHeader sentryTraceHeader = span.toSentryTrace();
            request.getHeaders().add(sentryTraceHeader.getName(), sentryTraceHeader.getValue());
            try {
                final ClientHttpResponse response = execution.execute(request, body);
                // handles both success and error responses
                span.setStatus(SpanStatus.fromHttpStatusCode(response.getRawStatusCode()));
                responseStatusCode = response.getRawStatusCode();
                return response;
            } catch (Exception e) {
                // handles cases like connection errors
                span.setThrowable(e);
                span.setStatus(SpanStatus.INTERNAL_ERROR);
                throw e;
            } finally {
                span.finish();
            }
        } finally {
            addBreadcrumb(request, body, responseStatusCode);
        }
    }

    private void addBreadcrumb(
        final @NotNull HttpRequest request,
        final @NotNull byte[] body,
        final @Nullable Integer responseStatusCode) {
        final Breadcrumb breadcrumb =
            Breadcrumb.http(request.getURI().toString(), request.getMethodValue(), responseStatusCode);
        breadcrumb.setData("requestBodySize", body.length);
        hub.addBreadcrumb(breadcrumb);
    }
}
